package org.kailu.book.controller;

import org.kailu.book.entity.AuthorEntity;
import org.kailu.book.service.AuthorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * @author viet.ch
 * @generated May 22, 2014
 * @from VN
 */

@Controller
public class AuthorController {

	@Autowired
	private AuthorService authorService;
	
	@RequestMapping("/author")
	private String getData(ModelMap map) {
		map.addAttribute("authorList", authorService.getAll());
		return "author";
	}
	
	@RequestMapping(value = "/author/saveorupdate", method = RequestMethod.GET)
	public String initData(ModelMap map) {
		map.addAttribute("author",new AuthorEntity());
		return "author_new";
	}
	
	@RequestMapping(value = "/author/saveorupdate", method = RequestMethod.POST)
    public String add(@ModelAttribute(value="author") AuthorEntity authorEntity, BindingResult result, ModelMap map)
    {
		authorService.saveOrUpdate(authorEntity);
		return "redirect:/author";
    }
	
	@RequestMapping(value = "/author/saveorupdate/{authorId}", method = RequestMethod.GET)
    public String update(@PathVariable("authorId") int authorId, ModelMap map)
    {
		map.addAttribute("author", authorService.get(authorId));
		return "author_new";
    }
	
	@RequestMapping(value = "/author/delete/{authorId}", method = RequestMethod.GET)
    public String delete(@PathVariable("authorId") int authorId)
    {
		this.authorService.delete(authorId);
		return "redirect:/author";
    }
}
